<?php

class CommentsController extends BookController
{
	public $layout=false;

        public function filters()
        {
                return array(
                        array(
                                'CHttpCacheFilter + counts',
                                'lastModifiedExpression'=>array($this,'setLastModified'),
                                'etagSeedExpression'=>array($this,'setEtag')
                        ),
                );
        }

	/**
	 * 发表评论/显示所有评论
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 */
	public function actionIndex($bookFlag, $chapterFlag) {
		if (isset($_POST['comment'])) {
			echo $this->saveComments($bookFlag, $chapterFlag);
		}
		else {
			echo $this->getComments($bookFlag, $chapterFlag);
		}
	}
	
	/**
	 * 章节评论数
	 * @param int $bookFlag
	 * @param int $chapterFlag
	 */
	public function actionCounts($bookFlag, $chapterFlag) {

		$cacheKey = $this->getCacheId('commentsCounts', $bookFlag.'_'.$chapterFlag);
		$commentsCountsJSONString = Yii::app()->cache->get($cacheKey);
		if ($commentsCountsJSONString==false) {
			$book = $this->getBookInfo($bookFlag);
			$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
			
			$commentsCounts = $this->getCommentsCounts($book->moduleId, $chapter['chapterId']);
			
			$commentsCountsJSONString = CJSON::encode($commentsCounts);
			Yii::app()->cache->set($cacheKey, $commentsCountsJSONString, 86000);
		}
		echo $commentsCountsJSONString;
	}

	/**
	 * 章节评论列表
	 * @param string $bookFlag
	 * @param string $chapterFlag
	 * @param int $sectionId
	 */
	public function actionSection($bookFlag, $chapterFlag, $sectionId) {

		$cacheKey = $this->getCacheId('sectionComments', $bookFlag.'-'.$chapterFlag.'-'.$sectionId);
		$sectionCommentsString = Yii::app()->cache->get($cacheKey);
	
		if ($sectionCommentsString===false) {
			$book = $this->getBookInfo($bookFlag);
			$bookId = $book->moduleId;
			$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
			$chapterId = $chapter['chapterId'];
			$sectionCommentsString = '';
			$sectionComments = Yii::app()->db->createCommand()->from('{{module_'.$bookId.'_comments}}')
															->where('sectionId=?', array($sectionId))
															->order('addTime desc')
															->queryAll();
			
			foreach( $sectionComments as $comment) {
				if (trim($comment['website'])=='') {
					$link = CHtml::encode($comment['name']);
				}
				else {
					$link = CHtml::link(CHtml::encode($comment['name']), $comment['website']);
				}
				$date = CHtml::tag('span', array('class'=>'meta'), date("Y-m-d H:i:s", $comment['addTime']));
				$content = "<dd>". CHtml::tag('p', array(), CHtml::encode($comment['comment'])) ."</dd>";
				$li_content = "<dl><dt>".$link.' '.$date."</dt>". $content ."</dl>";
				$sectionCommentsString .= CHtml::tag('li',array('class'=>'comment', 'id'=> $comment['commentId']), $li_content)."\n";
			}
			Yii::app()->cache->set($cacheKey, $sectionCommentsString, 86000);
		}
	
		echo $sectionCommentsString;
	}

	/**
	 * 原文
	 * @param string $bookFlag
	 * @param string $chapterFlag
	 * @param int $sectionId
	 */
	public function actionOriginal($bookFlag, $chapterFlag, $sectionId) {
		$cacheKey = $this->getCacheId('original', $bookFlag.'-'.$chapterFlag.'-'.$sectionId);
		
		$originalString = Yii::app()->cache->get($cacheKey);
		if ($originalString===false) {
			
			$book = $this->getBookInfo($bookFlag);
			$bookId = $book->moduleId;
			$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
			$chapterId = $chapter['chapterId'];
			$section = Yii::app()->db->createCommand()->from('{{module_'.$bookId.'_sections}}')
														->where('sectionId=?', array($sectionId))
														->queryRow();
			$originalString =  SectionHelper::formatSection($section, 'originalSection');
		}
		echo $originalString;
	}

        public function setEtag() {
		$etagName = 'comment_tag';
                if (false === ($etag = Yii::app()->cache->get($etagName))) {
                        $etag = time();
                        Yii::app()->cache->set($etagName, $etag, 3000);
                }
                return $etag;
        }

        public function setLastModified() {
		$lastName = 'comment_last';
                if (false === ($lastModified = Yii::app()->cache->get($lastName))) {
                        $lastModified = time();
                        Yii::app()->cache->set($lastName, $lastModified, 3000);
                }
                return $lastModified;
        }
}
